package com.workcell.workdata.xthings.mapper.mqtt;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.workcell.workdata.xthings.entity.mqtt.PowerMeterData;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Date;

/**
 * @Author 廖博文
 * @Date 2022/11/21 15:24
 * @Description 功率计数据
 */
@Mapper
public interface PowerMeterDataMapper extends BaseMapper<PowerMeterData> {

	/**
	 * 查询设备最近的一次记录
	 * @param deviceNo
	 * @param time
	 * @return
	 */
	@Select("select * from t_power_meter_data where id = (select MAX(id) from t_power_meter_data where slave_device_no = #{deviceNo}  AND `create_time` >= DATE_SUB(NOW(),INTERVAL #{time} MINUTE))")
	PowerMeterData getLatelyDataInfoByDeviceNo(@Param("deviceNo") String deviceNo, @Param("manufacturerCode") String manufacturerCode, @Param("time") Integer time);

	/**
	 * 获取硬件时间区间内的最近一笔数据
	 * @param deviceNo
	 * @return
	 */
	@Select("select * from t_power_meter_data where slave_device_no = #{deviceNo}  and `create_time` >= #{startTime} and `create_time` < #{endTime} order by `create_time` desc LIMIT 1;")
	PowerMeterData getDataHourLineChartInfo(@Param("deviceNo") String deviceNo, @Param("manufacturerCode") String manufacturerCode, @Param("startTime") Date startTime, @Param("endTime") Date endTime);

	@DS("slave_mqtt")
	@Select("select * from ${tableName} where manufacturer_code = #{manufacturerCode} and slave_device_no=#{slaveDeviceNo} and `create_time` >= #{startTime} and `create_time` <= #{endTime} order by `create_time` desc LIMIT 1;")
	PowerMeterData getFiveData(@Param("tableName") String tableName, @Param("manufacturerCode") String manufacturerCode, @Param("slaveDeviceNo") String slaveDeviceNo, @Param("startTime") Date startTime, @Param("endTime") Date endTime);

	@DS("slave_mqtt")
	@Select("select * from ${tableName} where manufacturer_code = #{manufacturerCode} and slave_device_no=#{slaveDeviceNo} and `create_time` >= #{startTime} order by `create_time` LIMIT 1 ")
	PowerMeterData getDataByStartTime(@Param("tableName") String tableName, @Param("manufacturerCode") String manufacturerCode, @Param("slaveDeviceNo") String slaveDeviceNo,  @Param("startTime") String startTime);

}
